package com.my.api.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.code.kaptcha.Producer;
import com.my.api.request.user.LoginRequest;
import com.my.api.request.user.RegisterRequest;
import com.my.biz.service.ICodeService;
import com.my.biz.service.IUserInfoService;
import com.my.util.BindingResultUtil;
import com.my.util.MsgUtil;

@Controller
@RequestMapping("passport")
public class PassportController {
	
	@Resource(name="kaptchaProducer")
	private Producer kaptchaProducer;
	
	@Autowired
	private IUserInfoService userInfoService;
	
	@Autowired
	private ICodeService codeService;

	@RequestMapping("register")
	@ResponseBody
	public Object register(@Valid RegisterRequest req,BindingResult result){
		try{
			if(result.hasErrors()){
				return MsgUtil.error( BindingResultUtil.getErrorParams(result));
			}
			if(!req.getAccount().endsWith("@qq.com")){
				return MsgUtil.error("目前仅支持QQ邮箱,请使用QQ邮箱!");
			}
			codeService.checkCode(req.getAccount()+"_mail", req.getCaptcha());
			return userInfoService.register(req);
		}catch(IllegalArgumentException e){
			return MsgUtil.error(Integer.valueOf(400), e.getMessage());
		}
		catch(Exception e){
			
			return MsgUtil.error(Integer.valueOf(500), "注册失败,请稍后再试");
		}
	}
	
	@RequestMapping("send_captcha")
	@ResponseBody
	public Object sendCaptcha(@RequestParam("mail")String mail){
		try{
			if(StringUtils.isBlank(mail)){
				return MsgUtil.error( "邮箱不能为空");
			}
			if(!mail.endsWith("@qq.com")){
				return MsgUtil.error("目前仅支持QQ邮箱,请使用QQ邮箱!");
			}
			return codeService.sendRegCodeEMailCode(mail);
		}catch(IllegalArgumentException e){
			return MsgUtil.error(Integer.valueOf(400), e.getMessage());
		}catch(Exception e){
			
			return MsgUtil.error(Integer.valueOf(500), "发送失败,请稍后再试");
		}
	}
	
	/*@RequestMapping("captcha.jpg")
	public void getCaptcha(@RequestParam("mail")String mail,HttpServletRequest req,HttpServletResponse resp) throws Exception{
				// Set to expire far in the past.
				resp.setDateHeader("Expires", 0);
				// Set standard HTTP/1.1 no-cache headers.
				resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
				// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
				resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
				// Set standard HTTP/1.0 no-cache header.
				resp.setHeader("Pragma", "no-cache");

				// return a jpeg
				resp.setContentType("image/jpeg");

				// create the text for the image
				String capText = this.kaptchaProducer.createText();


				
				
				codeService.addLoginCode(mail , capText);
				// create the image with the text
				BufferedImage bi = this.kaptchaProducer.createImage(capText);

				ServletOutputStream out = resp.getOutputStream();

				
				
				// write the data out
				ImageIO.write(bi, "jpg", out);
				
				

	}*/
	
	
	@RequestMapping("login")
	@ResponseBody
	public Object login(@Valid LoginRequest req,BindingResult result){
		try{
			if(result.hasErrors()){
				return MsgUtil.error( BindingResultUtil.getErrorParams(result));
			}
			if(!req.getAccount().endsWith("@qq.com")){
				return MsgUtil.error("目前仅支持QQ邮箱,请使用QQ邮箱!");
			}
			//codeService.checkCode(req.getAccount()+"_captcha", req.getCaptcha());
			return userInfoService.login(req);
		}catch(IllegalArgumentException e){
			return MsgUtil.error( e.getMessage());
		}
		catch(Exception e){
			return MsgUtil.error(Integer.valueOf(500), "登录失败,请稍后再试");
		}
	}
}
